package org.jeecg.modules.assets.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.assets.entity.AssetsDoubleQuotationResult;
import org.jeecg.modules.assets.entity.AssetsPreliminaryResults;
import org.jeecg.modules.assets.vo.DoubleEndVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.*;

@Component
@Slf4j
public class WechatUtils {

    @Autowired
    private RedisUtil redisUtil2;
    private static String corpsecret = "6XgfQgitojWheaAlKBNGTYXM2V2zN75omdZCJ9WC9TE";
    private static String agentid = "1000059";
    private static String agentid1 = "1000065";
    //飞科电器企业id
    public static final String corpid = "wx439ca03123165bdb";
    public static final String getAccessToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
    private static String sendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";

    public String sendWechatMessage(String username, String billNo, String endtim) {
        String token = getToken();
        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("msgtype", "miniprogram_notice");
        parmsMap.put("agentid", agentid);
        parmsMap.put("safe", 0);
        parmsMap.put("enable_id_trans", 0);
        parmsMap.put("enable_duplicate_check", 1);
        parmsMap.put("duplicate_check_interval", 14399);
        parmsMap.put("touser", username);
        Map<String, Object> miniprogram_notice = new HashMap<>();
        miniprogram_notice.put("appid", "wxd1d0005fc3e1862b");
        miniprogram_notice.put("page", "takeStock/initial/create?billNo=" + billNo);
        miniprogram_notice.put("title", LocalDate.now().getMonthValue() + "月-固定资产初盘通知");
        List<Map<String, Object>> content_item_list = new ArrayList<>();
        Map<String, Object> content_item = new HashMap<>();
        Map<String, Object> content_item1 = new HashMap<>();
        content_item1.put("key", "单据编号");
        content_item1.put("value", billNo);
        miniprogram_notice.put("content_item", content_item_list);
        content_item.put("key", "截止时间");
        content_item.put("value", endtim);
        content_item_list.add(content_item);
        content_item_list.add(content_item1);
        parmsMap.put("miniprogram_notice", miniprogram_notice);
        String senUrl = String.format(sendUrl, token);
        String parmsJson = new JSONObject(parmsMap).toJSONString();
        String post = com.xkcoding.http.HttpUtil.post(senUrl, parmsJson);

        log.info("res:" + parmsJson + "post:" + post);
        return post;
    }

    public String sendWechatEndMessage(String username, AssetsPreliminaryResults item) {
        String token = getToken();
        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("msgtype", "miniprogram_notice");
        parmsMap.put("agentid", agentid);
        parmsMap.put("safe", 0);
        parmsMap.put("enable_id_trans", 0);
        parmsMap.put("enable_duplicate_check", 1);
        parmsMap.put("duplicate_check_interval", 14399);
        parmsMap.put("touser", username);
        Map<String, Object> miniprogram_notice = new HashMap<>();
        miniprogram_notice.put("appid", "wxd1d0005fc3e1862b");
        miniprogram_notice.put("page", "takeStock/initial/create?billNo=" + item.getBillNo());
        miniprogram_notice.put("title", LocalDate.now().getMonthValue() + "月-固定资产初盘结果通知");
        List<Map<String, Object>> content_item_list = new ArrayList<>();
        Map<String, Object> content_item = new HashMap<>();
        Map<String, Object> content_item1 = new HashMap<>();
        Map<String, Object> content_item2 = new HashMap<>();
        Map<String, Object> content_item3 = new HashMap<>();
        content_item1.put("key", "单据编号");
        content_item1.put("value", item.getBillNo());
        content_item2.put("key", "部门名称");
        content_item2.put("value", item.getDeptName());
        content_item2.put("key", "异常数量");
        content_item2.put("value", item.getPreliminaryBadNum());
        miniprogram_notice.put("content_item", content_item_list);
        content_item.put("key", "资产数量");
        content_item.put("value", item.getAssetsNum());

        content_item_list.add(content_item1);
        content_item_list.add(content_item2);
        content_item_list.add(content_item);
        content_item_list.add(content_item3);
        parmsMap.put("miniprogram_notice", miniprogram_notice);
        String senUrl = String.format(sendUrl, token);
        String parmsJson = new JSONObject(parmsMap).toJSONString();
        String post = com.xkcoding.http.HttpUtil.post(senUrl, parmsJson);
        log.info("res:" + parmsJson + "post:" + post);
        return post;
    }

    /**
     * 抽盘企微发送
     *
     * @param username
     * @param billNo
     * @param assessName
     * @param roundName
     * @return
     */
    public String sendRoundmSotckWechatMessage(String username, String billNo, String assessName, String roundName) {
        String token = getToken();
        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("msgtype", "miniprogram_notice");
        parmsMap.put("agentid", agentid);
        parmsMap.put("safe", 0);
        parmsMap.put("enable_id_trans", 0);
        parmsMap.put("enable_duplicate_check", 1);
        parmsMap.put("duplicate_check_interval", 14399);
        parmsMap.put("touser", username);
        Map<String, Object> miniprogram_notice = new HashMap<>();
        miniprogram_notice.put("appid", "wxd1d0005fc3e1862b");
        miniprogram_notice.put("page", "takeStock/spotCheck/detail?billNo=" + billNo);
        miniprogram_notice.put("title", "固定资产抽盘异常通知");
        List<Map<String, Object>> content_item_list = new ArrayList<>();
        Map<String, Object> content_item = new HashMap<>();
        Map<String, Object> content_item1 = new HashMap<>();
        Map<String, Object> content_item2 = new HashMap<>();
        content_item1.put("key", "单据编号");
        content_item1.put("value", billNo);
        miniprogram_notice.put("content_item", content_item_list);
        content_item.put("key", "资产名称");
        content_item.put("value", assessName);
        content_item2.put("key", "抽盘人");
        content_item2.put("value", roundName);
        content_item_list.add(content_item);
        content_item_list.add(content_item1);
        content_item_list.add(content_item2);
        parmsMap.put("miniprogram_notice", miniprogram_notice);
        String senUrl = String.format(sendUrl, token);
        String parmsJson = new JSONObject(parmsMap).toJSONString();
        String post = com.xkcoding.http.HttpUtil.post(senUrl, parmsJson);

        log.info("res:" + parmsJson + "post:" + post);
        return post;
    }


    public String sendDoubleQuotaionWechatMessage(String username, String billNo, String num, String endTime) {
        String token = getToken();
        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("msgtype", "miniprogram_notice");
        parmsMap.put("agentid", agentid);
        parmsMap.put("safe", 0);
        parmsMap.put("enable_id_trans", 0);
        parmsMap.put("enable_duplicate_check", 1);
        parmsMap.put("duplicate_check_interval", 14399);
        parmsMap.put("touser", username);
        Map<String, Object> miniprogram_notice = new HashMap<>();
        miniprogram_notice.put("appid", "wxd1d0005fc3e1862b");
        miniprogram_notice.put("page", "takeStock/abnormal/create?billNo=" + billNo);
        miniprogram_notice.put("title", "固定资产异常复盘通知");
        List<Map<String, Object>> content_item_list = new ArrayList<>();
        Map<String, Object> content_item = new HashMap<>();
        Map<String, Object> content_item1 = new HashMap<>();
        Map<String, Object> content_item2 = new HashMap<>();
        content_item1.put("key", "单据编号");
        content_item1.put("value", billNo);
        miniprogram_notice.put("content_item", content_item_list);
        content_item.put("key", "异常数量");
        content_item.put("value", num);
        content_item2.put("key", "截止时间");
        content_item2.put("value", endTime);
        content_item_list.add(content_item);
        content_item_list.add(content_item1);
        content_item_list.add(content_item2);
        parmsMap.put("miniprogram_notice", miniprogram_notice);
        String senUrl = String.format(sendUrl, token);
        String parmsJson = new JSONObject(parmsMap).toJSONString();
        String post = com.xkcoding.http.HttpUtil.post(senUrl, parmsJson);
        log.info("res:" + parmsJson + "post:" + post);
        return post;
    }

    public String sendDoubleQuotaionWechatEndMessage(String username, AssetsDoubleQuotationResult item) {
        String token = getToken();
        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("msgtype", "miniprogram_notice");
        parmsMap.put("agentid", agentid);
        parmsMap.put("safe", 0);
        parmsMap.put("enable_id_trans", 0);
        parmsMap.put("enable_duplicate_check", 1);
        parmsMap.put("duplicate_check_interval", 14399);
        parmsMap.put("touser", username);
        Map<String, Object> miniprogram_notice = new HashMap<>();
        miniprogram_notice.put("appid", "wxd1d0005fc3e1862b");
        miniprogram_notice.put("page", "takeStock/abnormal/create?billNo=" + item.getBillNo());
        miniprogram_notice.put("title", "固定资产复盘结果通知");
        List<Map<String, Object>> content_item_list = new ArrayList<>();
        Map<String, Object> content_item = new HashMap<>();
        Map<String, Object> content_item1 = new HashMap<>();
        Map<String, Object> content_item2 = new HashMap<>();
        Map<String, Object> content_item3 = new HashMap<>();
        content_item1.put("key", "单据编号");
        content_item1.put("value", item.getBillNo());
        content_item2.put("key", "部门名称");
        content_item2.put("value", item.getDeptName());
        content_item3.put("key", "异常数量");
        content_item3.put("value", item.getDoubleQuotationBadNum());
        miniprogram_notice.put("content_item", content_item_list);
        content_item.put("key", "资产数量");
        content_item.put("value", item.getDoubleQuotationNum());
        content_item_list.add(content_item1);
        content_item_list.add(content_item2);
        content_item_list.add(content_item);
        content_item_list.add(content_item3);


        parmsMap.put("miniprogram_notice", miniprogram_notice);
        String senUrl = String.format(sendUrl, token);
        String parmsJson = new JSONObject(parmsMap).toJSONString();
        String post = com.xkcoding.http.HttpUtil.post(senUrl, parmsJson);
        log.info("res:" + parmsJson + "post:" + post);
        return post;
    }


    public String getToken() {
        String wxAccessToken = "";
        if (redisUtil2.get(corpid) != null) {
            wxAccessToken = redisUtil2.get(corpid).toString();
        } else {
            String str = com.xkcoding.http.HttpUtil.get(getAccessToken);
            if (StringUtils.isNotBlank(str)) {
                JSONObject s = JSONObject.parseObject(str);
                if ("0".equals(s.get("errcode").toString())) {
                    wxAccessToken = s.get("access_token").toString();
                    //  设置token缓存有效时间
                    redisUtil2.set(corpid, wxAccessToken);
                    redisUtil2.expire(corpid, 3600);
                }

            }
        }
        return wxAccessToken;
    }

    String   getToken2() {
        String wxAccessToken = "";
        if (redisUtil2.get(agentid1) != null) {
            wxAccessToken = redisUtil2.get(agentid1).toString();
        } else {
         String    tokenUrl=   "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + "nFoN_UEpnbBb_BtKqdDXb0pP4y5ajPgSKYZhHuHrc3w";
            String str = com.xkcoding.http.HttpUtil.get(tokenUrl);
            if (StringUtils.isNotBlank(str)) {
                JSONObject s = JSONObject.parseObject(str);
                if ("0".equals(s.get("errcode").toString())) {
                    wxAccessToken = s.get("access_token").toString();
                    //  设置token缓存有效时间
                    redisUtil2.set(agentid1, wxAccessToken);
                    redisUtil2.expire(agentid1, 3600);
                }

            }

        }
        return wxAccessToken;
    }

    /**
     * 异常消息发送
     * @param doubleEndVO
     */
    public String endDoubleEndAssets(DoubleEndVO doubleEndVO) {
        String token = getToken2();
        Map<String, Object> parmsMap = new HashMap<>();
        parmsMap.put("msgtype", "textcard");
        parmsMap.put("agentid", agentid1);
        parmsMap.put("enable_id_trans",0 );
        parmsMap.put("enable_duplicate_check", 1);
        parmsMap.put("duplicate_check_interval", 14399);
       // parmsMap.put("duplicate_check_interval", 14399);
        //parmsMap.put("touser", doubleEndVO.getUSERID());
        parmsMap.put("touser",doubleEndVO.getUSERID() );
        Map<String, Object> miniprogram_notice = new HashMap<>();
        parmsMap.put("textcard", miniprogram_notice);
       // miniprogram_notice.put("appid", "wxd1d0005fc3e1862b");
        miniprogram_notice.put("title", "考勤异常通知");
        miniprogram_notice.put("url", "http://192.168.21.39:10082/judge/judge.html");
        String description = "<div class=\"highlight\">您有考勤存在异常!</div> <div class=\"normal\">请登录OA尽快处理。</div>";
        miniprogram_notice.put("description", description);
        String senUrl = String.format(sendUrl, token);
        String parmsJson = new JSONObject(parmsMap).toJSONString();
        String post = com.xkcoding.http.HttpUtil.post(senUrl, parmsJson);
        log.info("res:" + parmsJson + "post:" + post);
        return post;
    }
}
